import React from 'react'
// ===================================================================== Temp
import SubmitForm from '#cpt/temp/SubmitForm'
// ===================================================================== Declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 			= $Lazy.load(()=>import('#tp/page/page-container'))
// 
const $cache 		= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== data
const width = '25%'
// ===================================================================== component
class Index extends React.Component{
	state = {
		pag: {},
		// 提交表单列表
		formData : [
			{ label:'设备名称', 			name:'device_name',		required:true,	 title:'设备信息', width },
			{ label:'设备编号', 			name:'device_number', 	required:true,	 width},
			{ label:'设备型号', 			name:'device_model',	required:true, width },
			{ label:'英文名称', 			name:'device_name_en', 	width },
			{ label:'设备类型', 			name:'device_type',		required:true,	 type:'select', loading:true, width },
			{ label:'联机类型', 			name:'rel_type',		required:true,	 type:'select', width },
			{ label:'金额', 				name:'buy_price',		required:true, width},
			{ label:'所属科室', 			name:'pgroup_id',		required:true,	 type:'select', idStr: 'uuid', loading:true, width},
			{ label:'购买日期', 			name:'buy_date',		required:true,	 type:'date-time', after:true, width },
			{ label:'启用日期', 			name:'enabeld_date',	required:true,	 type:'date-time', after:true, width },
			{ label:'设备状态', 			name:'device_status',	required:true,	 type:'select', loading:true, width },
			{ label:'负责人', 			name:'pinyin_name', 	required:true, 	width},
			{ label:'设备识别码', 		name:'id_code', 		required:true, width},
			{ label:'数据标识类型', 		name:'data_type',		required:true,	type:'select', loading:true, width },
			
			{ label:'供应商', 			name:'supplier', 		title: '供应商信息', width},
			{ label:'联系人', 			name:'supp_user', width},
			{ label:'联系电话', 			name:'supp_phone', width},
			{ label:'供应商联系地址', 	name:'supp_address', width},
			{ label:'维修联系人', 		name:'repair_man', width},
			{ label:'维修电话', 			name:'repair_phone', width},
			
			{ label:'出厂编号/系列号',	name:'serial_number', 	title:'厂家信息', width},
			{ label:'出厂日期', 			name:'serial_date', 	type:'date-time', after:true, width },
			{ label:'生产厂家', 			name:'serial_man', width},
			{ label:'厂家联系人', 		name:'serial_user', width},
			{ label:'联系电话', 			name:'serial_phone', width},
			{ label:'备注', 				name:'serial_memo', 	type:'textarea', full:true},
			
			{ label:'最低湿度',			name:'min_humidity', 	title:'使用环境', width},
			{ label:'最高湿度', 			name:'max_humidity', width},
			{ label:'最低温度', 			name:'min_temperature', width},
			{ label:'最高温度', 			name:'max_temperature', width},
			{ label:'功率', 				name:'power', width},
			{ label:'电压', 				name:'voltage', width},
		]
	}
	
	model = {}
	
	componentDidMount(){
		const { uuid } =  $fn.query()
		if(uuid){
			this.uuid = uuid
			this.submit = { uuid }
		}
	}
	
	getSelect = () => {
		const { formData } = this.state
		
		$cache.then( async f => {
			// 设备类型
			$fn.setRow( formData, 'device_type', await f.getDisCache( { code:43500 } ))
			// 联机类型
			$fn.setRow( formData, 'rel_type', await f.getDisCache( { code:43300 } ))
			// 设备状态
			$fn.setRow( formData, 'device_status', await f.getDisCache( { code:200 } ))
			// 数据标识类型
			$fn.setRow( formData, 'data_type', await f.getDisCache( { code:64000 } ))
			// 科室
			$fn.setRow( formData, 'pgroup_id', await f.getCache( { api:'ke_shi' } ))
			
			this.setState({ formData }, () => {
				if(this.uuid){
					$http.submit(this,'device/info', { param:{ uuid: this.uuid }, dataName:null }).then(data=>{
						this.formRef.setValue(data)
					})
				}
			})
		})
	}

	render(){
		const { uuid } = this
		return (
			<Page title={uuid ? '编辑设备' : '添加设备'}>
				{
					SubmitForm.call(this,{ 
						contentClassName	: 'plr10',
						msg					: uuid ? '修改' : '保存',
						okText				: uuid ? '修改' : '保存',
						labelWidth			: 's8', 
						btnSize				: 'large',
						scroll				: true,
						hideButton			: false,
						api					: uuid ? 'device/edit' : 'device/add',
						onForm				: this.getSelect,
						backRefresh			: true,
						onComplete			: ()=>{ $fn.dataRemove('device/select') }
					}) 
				}
			</Page>
		)
	}
}

export default Index